package com.gs.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.gs.common.core.redis.RedisCache;
import com.gs.service.MessageService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private RedisCache redisCache;

    @RabbitListener(queues = "gs.phone.code.queue")//监听
    @Override
    public void send(Map<String, String> msg) {
        //1.获取手机号和验证码
        String phone = msg.get("phone");
        String code = msg.get("code");
        System.out.println(phone+"验证码："+code);

        //2.将手机号和验证码存入redis
        redisCache.setCacheObject("user_regist_phone_code"+phone,code);

        //2. 配置文件
//        DefaultProfile profile = DefaultProfile.getProfile(
//                "default",
//                "LTAI5tA5rsVG3GUvJg2dzneG",
//                "l4Ei4tcYmJF3Rpuoigz84xpLjw9VUA");
//        //3. 客户端对象
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        //4. 创建请求
//        CommonRequest request = new CommonRequest();//通用请求
//        request.setMethod(MethodType.POST);//请求方式
//        request.setDomain("dysmsapi.aliyuncs.com");//服务地址
//        request.setVersion("2017-05-25");//版本时间
//        request.setAction("Sendsms");//请求类型
//
//        request.putQueryParameter("PhoneNumbers", phone);
//        request.putQueryParameter("SignName", "费企翔的博客");
//        request.putQueryParameter("TemplateCode", "SMS_461986483");
//        String str="{\"code\":\"" +code+ "\"}";
//        request.putQueryParameter("TemplateParam", str);
//
//        //5. 发
//        try {
//            CommonResponse response = client.getCommonResponse(request);
//            System.out.println(response.getData());
//        } catch (ClientException e) {
//            throw new RuntimeException(e);
//        }
//
    }
}
